package com.example.sss.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("toLogin")
    public String login()  {
        return "login";
    }

    @RequestMapping("submitLogin")
    public String submitLogin(String username, String password, HttpSession session)  {
        if ("admin".equals(username) && "admin".equals(password)) {
            System.out.println("合法用户");
            session.setAttribute("username",username + System.currentTimeMillis());
            return "redirect:/demo/result";
        } else {
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            return "redirect:/login/toLogin";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session)  {
        Object username = session.getAttribute("username");
        if (username != null) {
            session.removeAttribute("username");
        }
        return "redirect:/login/toLogin";
    }

}
